<?php
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../include/common.inc.php");
class dimport{
	var $dsql;
	function __construct() {
		$this->dsql = $GLOBALS['dsql'];
	}	
	function getHtml($path){
		return file_get_contents($path);
	}
	function getallcat($path){
		$content=$this->getHtml($path);	
		preg_match_all("/html\">(.*)<\/a> <span>/", $content, $bigcat);
		foreach($bigcat[1] as $k=>$b){
			preg_match("/".$b."([\s\S]*?)<dt/",$content,$smallcat);
			$s=$this->getsmallcat($smallcat[1]);
			$listdb[$k]['sub']=$s;
			$listdb[$k]['name']=$b;			
		}
		return $listdb;		
	}
	function getsmallcat($content){
		preg_match_all("/html\">(.*)<\/a>/", $content, $smallcat);
		return $smallcat[1];
	}
	function datahandle($allcat){
		helper('string');
		foreach($allcat as $r){
			$typename=$r['name'];
			$fup=$this->dsql->GetOne("SELECT * FROM `#@__arctype` WHERE typename='$typename'");
			$reid=$fup['id'];
			$topid=$fup['topid'];
			$sitepath=$fup['sitepath'];
			$channeltype=$fup['channeltype'];
			$tempindex=$fup['tempindex'];
			$templist=$fup['templist'];
			$temparticle=$fup['temparticle'];
			foreach($r['sub'] as $v){
			    $py=GetPinyin($v);
				$typedir=$fup['typedir'].'/'.$py;
				$data[]=array(
					'reid'=>$reid,
					'topid'=>$topid,
					'typename'=>$v,					
					'typedir'=>$typedir,					
					'isdefault'=>1,
					'issend'=>1,
					'channeltype'=>$channeltype,
					'tempindex'=>$tempindex,
					'templist'=>$templist,
					'temparticle'=>$temparticle,
					'namerule'=>'{typedir}/{Y}/{M}{D}/{aid}.html',
					'namerule2'=>'{typedir}/list_{tid}_{page}.html'	,
					'sitepath'=>$sitepath				
				);			
			}			
		}
		return $data;
	}
	function insertData($data){
		foreach($data as $r){
			$sql="INSERT INTO #@__arctype(reid,topid,typename,typedir,isdefault,issend,channeltype,tempindex,templist,temparticle,namerule,namerule2,sitepath) VALUES('$r[reid]','$r[topid]','$r[typename]','$r[typedir]','$r[isdefault]','$r[issend]','$r[channeltype]','$r[tempindex]','$r[templist]','$r[temparticle]','$r[namerule]','$r[namerule2]','$r[sitepath]')";
			$this->dsql->ExecuteNoneQuery($sql);			
		}
		exit('success');
	}
}
$obj=new dimport();
$path="http://localhost:8102/dede/dcontent.html";
$allcat = $obj->getallcat($path);
$data=$obj->datahandle($allcat);
$obj->insertData($data);
?>
